<?php
/**
 * Created by PhpStorm.
 * User: hongtianjin
 * Date: 2017/3/1 0001
 * Time: 17:17
 */

namespace App\Models\ActivityPoints;

use App\Contracts\Repositories\ActivityPointsRepository as ActivityPointsRepositoryContracts;

class ActivityPointsRepository implements ActivityPointsRepositoryContracts
{
    public function findById($pointId)
    {
        return ActivityPoints::query()->where(['id'=>$pointId])->select('point_name')->get()->toArray();
    }
    public function findByConditions($conditions, $withs)
    {
    }
    public function findAll()
    {
        return ActivityPoints::query()->get();
    }
    public function create(ActivityPoints $activityPoints)
    {
        if (!is_null($activityPoints->id)) {
            throw new \Exception('duplicate id');
        }
        if ($activityPoints->save()) {
            return $activityPoints;
        }
        return null;
    }
    /**
     * @param array $activityPoints
     * @param array $conditions
     * @return mixed
     */
    public function modify($activityPoints, $conditions)
    {
        return ActivityPoints::where($conditions)->update($activityPoints);
    }
    public function delete($condition, $softDeletion)
    {
        if ($softDeletion == true) {
            return ActivityPoints::where($condition)->update(array('deleted_at' => date('Y-m-d H:i:s')));
        } else {
            return ActivityPoints::where($condition)->delete();
        }
    }


    public function findByConditionsPage($condition, $sortArray=array(), $start = 0, $limit = 10, $in=[], $action='')
    {
        if (!empty($in) || $action=='new_point') {
            $qb = ActivityPoints::query()->where($condition)->whereIn('id', $in)->limit($limit)->skip($start);
        } else {
            $qb = ActivityPoints::query()->where($condition)->limit($limit)->skip($start);
        }
        if (!empty($sortArray)) {
            foreach ($sortArray as $sort) {
                $qb = $qb->orderBy($sort[0], $sort[1]);
            }
        }
        return $qb->get();
    }

    public function countByConditions($condition, $in=[], $action='')
    {
        if (!empty($in) || $action=='new_point') {
            $count = ActivityPoints::query()->where($condition)->whereIn('id', $in)->count();
        } else {
            $count = ActivityPoints::query()->where($condition)->count();
        }
        return $count;
    }

    public function deleteByIds($ids)
    {
        return ActivityPoints::destroy($ids);
    }
}
